import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import rsa from '@/utils/rsa'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    salt: '',
    avatar: '',
    roles: [],
    permissions: []
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_SALT: (state, token) => {
    state.salt = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_PERMISSIONS: (state, permissions) => {
    state.permissions = permissions
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: rsa.rsaPublicData(password), agent: 'pc', clientId: '08ff2fbe48454a7faade093dcf78070a', grantType: 'password', type: '' }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.accessToken)
        commit('SET_SALT', data.slt)
        setToken(data.accessToken)
        setToken(data.slt, 'slt')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo().then(response => {
        const user = response.data
        console.log(response)

        if (user && user.length > 0) {
          const roles = []
          user.forEach(element => {
            roles.push(element.id)
          })
          commit('SET_ROLES', roles)
        } else {
          commit('SET_ROLES', ['ROLE_DEFAULT'])
        }
        commit('SET_NAME', user.account)
        commit('SET_PERMISSIONS', [])
        // commit('SET_ROLES', ['ROLE_DEFAULT'])
        // commit('SET_NAME', 'ADMIN')
        commit('SET_AVATAR', 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1113%2F092919113248%2F1Z929113248-8-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648352011&t=5ecb87e6ba40288bc4666a9b7505d053')
        resolve(response)
        resolve(user)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('SET_ROLES', [])
        commit('SET_PERMISSIONS', [])
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

